package office.service.hr.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import office.action.hr.form.QuitEntryForm;
import office.bo.hr.impl.QuitEntry;
import office.bo.system.Department;
import office.domainset.BizDomainProviderSetImpl;
import office.service.hr.IQuitEntryService;
import office.service.system.ISystemService;
import platform.common.exception.BusinessException;
import platform.common.service.impl.BaseBizServiceImpl;
import platform.util.Util;

public class QuitEntryServiceImpl extends BaseBizServiceImpl implements IQuitEntryService{

	/**
	 * 获取辞职申请信息
	 * @return
	 * @throws BusinessException
	 */
	@SuppressWarnings("unchecked")
	public List getQuitEntryList(QuitEntryForm objctForm) throws BusinessException{
		List listData = new ArrayList();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Map map = new HashMap();
			map.put("userId", objctForm.getUserId());
			map.put("checkRole", objctForm.getCheckRole());
			List<Object> deptId = null;
			if(objctForm.getRoleDFlag().equals("0")){
				deptId = systemService.getDescendantsId(objctForm.getDeptId());
			}else if(objctForm.getRoleDFlag().equals("1")){
				deptId = systemService.getDepartment(objctForm.getDeptId(),objctForm.getCompanyId());
			}
			if(null==objctForm.getCheckRole() || ("").equals(objctForm.getCheckRole())){
				throw new BusinessException("用户未设置相关角色!");
			}else{
				map.put("deptId", deptId);
				listData = domainProviderSetImpl.getDao().findByHqlNameAndDomian(domainProviderSetImpl.getQuitEntryDP(), "findByUserIdCheckRoleDept",map);
			}
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取转正申请列表信息失败!", e);
		}
		return listData;
	}
	/**
	 * 编辑辞职申请
	 * @throws BusinessException
	 */
	public void editQuitEntry(QuitEntryForm objctForm)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			QuitEntry bo = null;
			if(null==objctForm.getId() || ("").equals(objctForm.getId())){
				bo = (QuitEntry)domainProviderSetImpl.getQuitEntryDP().newEmptyObj();
			}else{
				bo = (QuitEntry)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getQuitEntryDP(), objctForm.getId());
			}
			bo =(QuitEntry) objctForm.copyPropertiesToBO(bo);// 拷贝了申请数量、描述
			if(null==bo.getId() || ("").equals(bo.getId())){
				bo.generatorUUID();
				bo.setUserId(objctForm.getUserId());
				
				bo.setState("0");//等待审批
				Calendar c = Calendar.getInstance();
				SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd");
				bo.setEntryDate(dateFmt.format(c.getTime()));
				bo.setEntryRole(objctForm.getEntryRole());
			}
			//验证是否存在重复记录
			bo.store(domainProviderSet);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("保存辞职申请信息失败!", e);
		}
	}
	/**
	 * 根据id获取对象
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	public QuitEntryForm getById(String id)throws BusinessException{
		QuitEntryForm form = new QuitEntryForm();//判断form的id属性是否存在,不存在则暂时不存在辞职申请
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			//根据用户信息获取辞职申请,没有获取到数据说明不存在辞职申请,新建辞职申请
			Map map = new HashMap();
			map.put("id", id);
			List list = domainProviderSetImpl.getDao().findByHqlNameAndDomian(domainProviderSetImpl.getQuitEntryDP(), "findById", map);
			if(list.size()>0){
				form = (QuitEntryForm)list.get(0);
			}
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("编辑申请办公用品信息失败!", e);
		}
		return form;
		
		
	}
	
	/**
	 * 根据用户id获取对象
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	public QuitEntryForm getByUserId(String userId)throws BusinessException{
		QuitEntryForm form = new QuitEntryForm();//判断form的id属性是否存在,不存在则暂时不存在辞职申请
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			//根据用户信息获取辞职申请,没有获取到数据说明不存在辞职申请,新建辞职申请
			Map map = new HashMap();
			map.put("userId", userId);
			List list = domainProviderSetImpl.getDao().findByHqlNameAndDomian(domainProviderSetImpl.getQuitEntryDP(), "findQuitEntryByUserId", map);
			if(list.size()>0){
				form = (QuitEntryForm)list.get(0);
			}
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("编辑申请办公用品信息失败!", e);
		}
		return form;
		
		
	}
	/**
	 * 根据id删除对象
	 * @param id
	 * @throws BusinessException
	 */
	public void deleteById(String id)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			domainProviderSetImpl.getDao().deleteById(domainProviderSetImpl.getQuitEntryDP(), id);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id删除辞职申请信息对象失败!", e);
		}
	}
	/**
	 * 提交审批
	 * @param id
	 * @throws BusinessException
	 */
	public void submitCheck(String id) throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			QuitEntry bo = (QuitEntry)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getQuitEntryDP(), id);
			bo.setState("1");
			String nextRole = getNextCheckRole(bo.getEntryRole(),bo.getCheckRole());
			bo.setCheckRole(nextRole);
			bo.store(domainProviderSet);//保存对象
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("提交审批失败!", e);
		}
	}
	/**
	 * 
	 * @param check 审批是否为通过
	 * @param id
	 * @throws BusinessException
	 */
	public void checkData(boolean check,String id)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			QuitEntry bo = (QuitEntry)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getQuitEntryDP(), id);
			if(check){
				String nextRole = getNextCheckRole(bo.getEntryRole(),bo.getCheckRole());
				if(nextRole.equals("end")){
					bo.setState("100");
					bo.setCheckRole("");
				}else{
					bo.setCheckRole(nextRole);
				}
			}else{
				bo.setCheckRole("");//重新提交审批,CheckRole重新赋值
				bo.setState("-1");//审批驳回
			}
			bo.store(domainProviderSet);//保存对象
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("设置审批状态信息对象失败!", e);
		}
	}
	/**
	 * 
	 * @param key 配置文件key
	 * @param preRole 现在所处审批角色
	 * @return
	 * @throws BusinessException
	 */
	public String getNextCheckRole(String entryRole,String preRole)throws BusinessException{
		String roles = Util.parsePropertiesReturnStr("office", "quitEntry-"+entryRole);
		String role[] = roles.split(",");
		if(null==preRole || ("").equals(preRole)){
			if(role.length<2){
				throw new BusinessException("未找到下级审批节点,审批流程配配置错误");
			}
			return role[1];
		}
		if(roles.indexOf(",")>-1){
			if(roles.endsWith(","+preRole)){
				return "end";
			}else if(roles.endsWith(preRole)){
				return "end";
			}
		}
		
		for(int i=0; i<role.length;i++){
			if(role[i].equals(preRole)){
				return role[i+1];
			}
		}
		return "";
	}
	@Resource
	private ISystemService systemService;
	public ISystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(ISystemService systemService) {
		this.systemService = systemService;
	}
}
